package com.chixing.ih.module.admin.controller;

import com.chixing.ih.json.ResponseData;
import com.chixing.ih.module.admin.service.IAdminTaskService;
import com.chixing.ih.module.admin.service.IAdminUserService;
import com.chixing.ih.module.task.pojo.Task;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("ATC")
@Api(tags = "管理员任务相关Api")
public class AdminTaskCon {

    @Autowired
    private IAdminTaskService adminTaskService;

    @Autowired
    private IAdminUserService adminUserService;


    @ApiOperation(value = "查看等待审核任务接口", notes = "查看待管理员审核的任务数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示条数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "keyword", value = "搜索关键字", required = true, dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
    })
    @GetMapping("getPendingTasks")
    public Map<String, Object> getPendingTasks(Integer pageNum, Integer pageSize, String keyword) {
        List<Task> pendingTasks = adminTaskService.getPendingTasks(pageNum, pageSize, keyword);
        PageInfo<Task> info = new PageInfo<>(pendingTasks);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "查看未通过审核任务接口", notes = "查看未通过审核任务数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示条数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "keyword", value = "搜索关键字", required = true, dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
    })
    @GetMapping("getRefusedTasks")
    public Map<String, Object> getRefusedTasks(Integer pageNum, Integer pageSize, String keyword) {
        List<Task> refusedTasks = adminTaskService.getRefusedTasks(pageNum, pageSize, keyword);
        PageInfo<Task> info = new PageInfo<>(refusedTasks);
        return ResponseData.REP_GET_SUCCESS(info);
    }

    @ApiOperation(value = "查看已审核任务接口", notes = "查看管理员已审核的任务数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示条数", required = true, dataType = "Integer")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
    })
    @GetMapping("getAuditedTasks")
    public Map<String, Object> getAuditedTasks(Integer pageNum, Integer pageSize, String keyword) {
        List<Task> auditedTasks = adminTaskService.getAuditedTasks(pageNum, pageSize, keyword);
        PageInfo<Task> info = new PageInfo<>(auditedTasks);
        return ResponseData.REP_GET_SUCCESS(info);
    }


    @ApiOperation(value = "任务通过审核", notes = "管理员同意任务过审")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "任务Id", required = true, dataType = "Integer"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
    })
    @GetMapping("applicationApproved/{taskId}/{userId}")
    public boolean applicationApproved(@PathVariable("taskId") Integer taskId, @PathVariable("userId") Integer userId) {
        if (adminTaskService.applicationApproved(taskId)) {
            String mail = adminUserService.getUserMail(userId);
            return true;
        }
        return false;
    }


    @ApiOperation(value = "任务拒绝通过审核", notes = "管理员拒绝任务过审")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "任务Id", required = true, dataType = "Integer"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "拒绝成功"),
    })
    @GetMapping("rejectApplication/{taskId}/{userId}")
    public boolean rejectApplication(@PathVariable("taskId") Integer taskId, @PathVariable("userId") Integer userId) {
        if (adminTaskService.rejectApplication(taskId)) {
            return true;
        }
        return false;
    }


    @ApiOperation(value = "重置任务审核状态", notes = "将审核过的任务重新转为待审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "任务Id", required = true, dataType = "Integer"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "重置成功"),
    })
    @GetMapping("resetApplication/{id}")
    public boolean resetApplication(@PathVariable("id") Integer id) {
        return adminTaskService.resetApplication(id);
    }


}
